package com.kdtech.analyse.news;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class EasternTimesNewsAnalyse implements AnalyseNews {
	private static final String[] regex={
		"http://www.eastern-times.com/news/html/[?][0-9]*.html",
	};	
	
	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}		
		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();	
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);				
		news.setUrl(url);	
		
		title =doc.select("td.newstitle").text();
		if(StringUtils.isBlank(title)){
			title =doc.select(".newstitle").text();
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("td.newscon"));
		date=DateUtils.matchDate(doc.select("td.newsinfo").text());
		//author = doc.select("td.newsinfo").text();
		author=JSoupUtils.matchAuthor(doc, "作者：");
		
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);	
		news.setType(1);	
		return news;
	}	
	public static void main(String[] args) {
		String url="http://www.eastern-times.com/news/html/?1276.html";
		EasternTimesNewsAnalyse analyse=new EasternTimesNewsAnalyse();		
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
